<?php
require_once 'config/database.php';

echo "<h2>数据库初始化</h2>";

// 删除已存在的表
$conn->query("DROP TABLE IF EXISTS production_details");
$conn->query("DROP TABLE IF EXISTS users");

echo "正在创建数据表...<br>";

// 创建用户表
$sql = "CREATE TABLE users (
    id INT(11) NOT NULL AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(255) NOT NULL,
    role ENUM('admin','user') NOT NULL DEFAULT 'user',
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";

if ($conn->query($sql)) {
    echo "✅ 用户表创建成功<br>";
} else {
    echo "❌ 创建用户表失败: " . $conn->error . "<br>";
    die();
}

// 创建生产明细表
$sql = "CREATE TABLE production_details (
    id INT(11) NOT NULL AUTO_INCREMENT,
    model_number VARCHAR(50) NOT NULL COMMENT '型号',
    production_batch VARCHAR(100) NOT NULL COMMENT '生产批次',
    machine_number VARCHAR(50) NOT NULL COMMENT '整机编号',
    compatible_model VARCHAR(100) NOT NULL COMMENT '适配型号',
    production_status VARCHAR(50) NOT NULL COMMENT '生产进度',
    is_number_changed ENUM('是','否') NOT NULL DEFAULT '否' COMMENT '是否改车号',
    original_model VARCHAR(50) COMMENT '原机型',
    original_batch VARCHAR(100) COMMENT '原批次',
    original_machine_number VARCHAR(50) COMMENT '原整机编号',
    preprocess_online_date DATE COMMENT '预处理上线',
    preprocess_storage_date DATE COMMENT '预处理入库',
    core_offline_date DATE COMMENT '机芯下线',
    core_coating_date DATE COMMENT '机芯转涂',
    core_storage_date DATE COMMENT '机芯入库',
    shell_install_date DATE COMMENT '壳体安装',
    hammer_coating_date DATE COMMENT '整锤转涂',
    hammer_storage_date DATE COMMENT '整捶入库',
    delivery_date DATE COMMENT '发车',
    sales_type VARCHAR(50) COMMENT '销售类型',
    compatible_machine_type VARCHAR(100) COMMENT '适配机型',
    notes TEXT COMMENT '备注',
    hammer_storage_month INT COMMENT '整捶入库月份',
    hammer_storage_year INT COMMENT '整捶入库年份',
    delivery_month INT COMMENT '发车月份',
    delivery_year INT COMMENT '发车年份',
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";

if ($conn->query($sql)) {
    echo "✅ 生产明细表创建成功<br>";
} else {
    echo "❌ 创建生产明细表失败: " . $conn->error . "<br>";
    die();
}

// 创建管理员账户
echo "正在创建管理员账户...<br>";
$admin_username = 'admin';
$admin_password = password_hash('admin123', PASSWORD_DEFAULT);

$sql = "INSERT INTO users (username, password, role) VALUES ('$admin_username', '$admin_password', 'admin')";

if ($conn->query($sql)) {
    echo "✅ 管理员账户创建成功<br>";
    echo "👤 用户名: admin<br>";
    echo "🔑 密码: admin123<br>";
} else {
    echo "❌ 创建管理员账户失败: " . $conn->error . "<br>";
}

// 插入测试数据
echo "正在插入测试数据...<br>";

// 插入生产明细测试数据
$sql = "INSERT INTO production_details (
    model_number, production_batch, machine_number, compatible_model, 
    production_status, is_number_changed, preprocess_online_date, 
    preprocess_storage_date, hammer_storage_date, delivery_date,
    hammer_storage_month, hammer_storage_year, delivery_month, delivery_year
) VALUES 
    ('XAB210S.00', 'W20240501-XEB210S-2/4', 'XAB210SRE00001', 'XE650GK', 
    '整锤入库', '否', '2025-01-13', '2025-01-13', '2024-07-20', NULL,
    7, 2024, NULL, NULL),
    ('XAB210S.00', 'W20240501-XEB210S-2/4', 'XAB210SRE00002', 'XE650GK', 
    '整锤入库', '是', '2025-01-14', '2025-01-14', '2024-07-20', NULL,
    7, 2024, NULL, NULL),
    ('XAB215S.00', 'W20240603-XAB215S-3/5', 'XAB215SRF00002', 'XE650GK/XE690DK', 
    '发车', '是', '2025-01-14', '2025-01-14', '2024-07-09', '2024-09-04',
    7, 2024, 9, 2024)";

if ($conn->query($sql)) {
    echo "✅ 测试明细数据插入成功<br>";
} else {
    echo "❌ 测试明细数据插入失败: " . $conn->error . "<br>";
}

echo "<br>✅ 数据库初始化完成！<br>"; 